package com.twu.biblioteca;

import java.io.PrintStream;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: anubhav
 * Date: 1/3/14
 * Time: 5:10 PM
 * To change this template use File | Settings | File Templates.
 */
public class BookIssuer implements Action {

    private Map<String, LibraryItem> availableBooks;
    private Map<String, IssuedBook> issuedBooks;
    private PrintStream out;
    private InputSeeker in;
    private String customerNumber;

    public BookIssuer(Map<String, LibraryItem> avlist, Map<String, IssuedBook> issuelist, PrintStream out,
                      InputSeeker in, String libNumber) {
        availableBooks = avlist;
        issuedBooks = issuelist;
        this.out = out;
        this.in = in;
        this.customerNumber = libNumber;
    }

    @Override
    public void executeAction() {
        AvailibilityChecker checker = new AvailibilityChecker(availableBooks);
        out.println("Please enter book name:");
        String bookName = in.takeStringInput();
        if (checker.checkForItem(bookName)) {
            issue(bookName);
            out.println("Thank you! Enjoy the book");
        } else
            out.println("That book is not available.");
    }


    private void issue(String bookName) {

        issuedBooks.put(bookName, new IssuedBook((Book)availableBooks.get(bookName), customerNumber));
        availableBooks.remove(bookName);

    }
}
